project(Plugins)
cmake_minimum_required(VERSION 3.24)

set(target_name ExternalRenderPlugin)

set(tflex_cad_path "C:/TopSystems/CAD/Dev/Bin64" CACHE FILEPATH "TFlex path")
set(tflex_plugin_install_path "${tflex_cad_path}" CACHE FILEPATH "Plugin install path")

find_package(OpenGL REQUIRED)

set(tflex_cad_libs "${tflex_cad_path}/TFlexAPI.dll;
			        ${tflex_cad_path}/TFlexAPI3D.dll;
		            ${tflex_cad_path}/TFlexAPIData.dll;
		            ${tflex_cad_path}/TFPSSchemeEditor.dll")

set(src "Plugin.h"
	    "PluginFactory.cpp"
	    "PluginFactory.h"
	    "RenderAPI.h"
	    "Global.h"
	    "OpenGLRender.cpp"
	    "OpenGLRender.h"
	    "Plugin.cpp")

add_library(${target_name} SHARED ${src})

target_compile_options(${target_name} PRIVATE /clr)
target_compile_options(${target_name} PRIVATE /fp:precise) # /fp:strict is incompatible with /clr

set_target_properties(${target_name} PROPERTIES
	VS_GLOBAL_CLRSupport "true"
	VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.7.2"
	VS_DOTNET_REFERENCES "System;
						  System.Core;
                          System.Data;
                          System.Data.DataSetExtensions;
						  System.Windows.Forms;
						  WindowsBase;
                          ${tflex_cad_libs}"
)

target_link_libraries(${target_name} opengl32)
target_include_directories(${target_name} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# Note: Modification of compiler flags is required for CLR compatibility now that we are using .resx files.
string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")

install(TARGETS ${target_name} DESTINATION ${tflex_plugin_install_path})
